# Create your views here.
from django.http import HttpResponse
import os
from django.shortcuts import render
import operator
import te
import docker

log_dir='/var/log/apache2'
def base(request):
 return render(request,"base.html")
def run(request):
 client=docker.from_env()
 images=client.images.list()
 return render(request,"run.html",{"images":images})
def runc(request):
 client=docker.from_env()
 request.encoding='utf-8'
 restart=request.POST["restart"].encode('utf-8')
 cname=request.POST["cname"].encode('utf-8')
 image=request.POST["image"].encode('utf-8').split("'")[1]
 print image
 print restart
 print cname
 client.containers.run(image=image,name=cname,detach=True,tty=True,restart_policy={"name":restart,"MaximumRetryCount":3})
 return render(request,"stop.html")
def index(request):
 return HttpResponse("hello world !")
def list(request):
 str="helloworld"
 client=docker.from_env()
 containers=client.containers.list()
 html="<html><title>hh</title><body>%s</body></html>" %containers
 return HttpResponse(html)
def hi(request):
 client=docker.from_env()
 containers=client.containers.list("before")
 print request.path
 return render(request,"list.html",{"containers":containers})
def container(request,id):
  client=docker.from_env()
  path=request.path
  id=path.split("container_")[1]
  print id
  container=client.containers.get(id)
  log=container.logs()
  return render(request,"container.html",{"container":container,"log":log})
def constop(request,id):
  client=docker.from_env()
  path=request.path
  id=path.split("stop_")[1]
  container=client.containers.get(id) 
  result="run"
  print container.status
  container.stop()
  result=id+" already stop"
  print id
  print "stop"
  return render(request,"stop.html",{"result":result})
def constart(request,id):
  client=docker.from_env()
  path=request.path
  id=path.split("start_")[1]
  container=client.containers.get(id)
  if container.status == "exited":
   print "run"
   container.start()
   result=id+" already start"
  else: 
   result="cannot start"
  return render(request,"stop.html",{"result":result})
def conkill(request,id):
  client=docker.from_env()
  path=request.path
  id=path.split("kill_")[1]
  container=client.containers.get(id)
  container.kill()
  result=id+" already kill"
  return render(request,"stop.html",{"result":result})
def conremove(request,id):
  client=docker.from_env()
  path=request.path
  id=path.split("_")[1]
  container=client.containers.get(id)
  container.remove()
  result=id+" already move"
  return render(request,"stop.html",{"result":result})
def conrename(request,id):
  client=docker.from_env()
  path=request.path
  id=path.split("_")[1]
  newname=request.POST["newname"].encode('utf-8')
  print id
  print newname
  container=client.containers.get(id)
  container.rename(newname)
  result=id+" already rename"
  return render(request,"stop.html",{"result":result})
def i_list(request):
  client=docker.from_env()
  images=client.images.list()
  print images
  return render(request,"i_list.html",{"images":images})
def i_rm(request,id):
  path=request.path
  id=path.split(":")[1]
  client=docker.from_env()
  client.images.remove(id,True)
  result="This image is removed"
  return render(request,"stop.html",{"result":result})
def i_show(request,id):
  client=docker.from_env()
  path=request.path
  id=path.split(":")[1]
  image=client.images.get(id)
  history=image.history()
  print image
  return render(request,"i_show.html",{"image":image,"history":history})
def i_reload(request,id):
  client=docker.from_env()
  path=request.path
  id=path.split(":")[1]
  image=client.images.get(id)
  i= image.reload()
  result="The images of"+id
  print  i
  return render(request,"stop.html",{"result":result})
def v_list(request):
  client=docker.from_env()
  volumes=client.volumes.list()
  return render(request,'v_list.html',{"volumes":volumes})
def v_show(request,id):
  client=docker.from_env()
  path=request.path
  id=path.split("_")[1]
  volume=client.volumes.get(id)
  print id
  return render(request,"v_show.html",{"volume":volume})
def v_rm(request,id):
  client=docker.from_env()
  path=request.path
  id=path.split("_")[1]
  volume=client.volumes.get(id)
  volume.remove(force=False) 
  result="this volume has deleted"
  return render(request,"stop.html",{"result":result})
def vc(request):
 return render(request,"v_create.html")
def vcc(request):
 client=docker.from_env() 
 name=request.POST["name"].encode('utf-8')
 labels=request.POST["lab"].encode('utf_8')
 vloume=client.volumes.create(name=name,labels={"labels":labels})
 result=name+"has created"
 return render(request,"stop.html",{"result":result})
def n_list(request):
  client=docker.from_env()
  networks=client.networks.list()
  return render(request,'n_list.html',{"networks":networks})
